package com.qfedu.wfxvip.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qfedu.wfxindex.JsonResultVO;
import com.qfedu.wfxvip.po.Shoppingcar;
import com.qfedu.wfxvip.po.WxbGood;
import com.qfedu.wfxvip.service.impl.GoodServiceImpl;
import com.qfedu.wfxvip.vo.MerchantInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.logging.Logger;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodServiceImpl igoodService;
    @Autowired
    private RestTemplate restTemplate;

    private Logger logger = Logger.getLogger("中老年保健商城");

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<WxbGood> queryGoodsList(){
        logger.info("--------中老年保健---------");
        List<WxbGood> goodList = igoodService.queryAllGoods();
        return goodList;
    }

    @RequestMapping("/details")
    public String queryGoodsDetailsByGoodsId(Model model,String goodsId){
        WxbGood wxbGood = igoodService.queryGoodsById(goodsId);
        model.addAttribute("goods",wxbGood);
        return "goods_details";
    }

    @RequestMapping("/addshopping")
    @HystrixCommand(fallbackMethod = "addShopping1")
    @ResponseBody
    public JsonResultVO addShopping(String goodsId, String sku){
        Shoppingcar shoppingcar = new Shoppingcar();
        shoppingcar.setShpGoodsId(goodsId);
        shoppingcar.setShpGoodsNum(1);
        shoppingcar.setShpGoodsSku(sku);
        shoppingcar.setShpUserId("47376995");
        shoppingcar.setShpMerchantId("69609206");
//        JsonResultVO jsonResultVO = new JsonResultVO();
//        try {
//            iShoppingService.addShopping(shoppingcar);
//            jsonResultVO.setCode(1);
//        } catch (Exception e) {
//            e.printStackTrace();
//            jsonResultVO.setCode(0);
//        }
        JsonResultVO jsonResultVO = restTemplate.postForObject("http://wfx-cart1/add", shoppingcar, JsonResultVO.class);
        return jsonResultVO;
    }

    public JsonResultVO addShopping1(String goodsId, String sku){
        JsonResultVO jsonResultVO = new JsonResultVO();
        jsonResultVO.setCode(0);
        jsonResultVO.setMsg("bj----触发熔断机制");
        return jsonResultVO;
    }

    @RequestMapping("/shoppingList")
    public String queryShoppingList(Model model){
//        List<MerchantInfo> merchantInfos = iShoppingService.queryShoppingList();
        MerchantInfo[] list = restTemplate.getForObject("http://wfx-cart1/list", MerchantInfo[].class);
        model.addAttribute("list",list);
        return "shoppingcart";
    }

}
